﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Transactions;

namespace BusinessLogic
{
    public static class CongNo
    {
        #region khởi tạo
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;
        static Entities.KhachHang[] khachhang;
        static Entities.NhaCungCap[] nhacungcap;
        #endregion

        #region Select
        public static Entities.SoDuCongNo[] Select(string makho)
        {
            try
            {
                string sql = Common.Constants.CongNo.SelectSoDuCongNoTheoKho;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.CongNo.MaKho, SqlDbType.VarChar, 50).Value = makho;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.SoDuCongNo soducongno = new Entities.SoDuCongNo();
                    soducongno.SoDuCongNoID = Convert.ToInt32(dr[Common.Constants.CongNo.SoDuCongNoID].ToString());
                    soducongno.MaSoDuCongNo = dr[Common.Constants.CongNo.MaSoDuCongNo].ToString();
                    soducongno.MaDoiTuong = dr[Common.Constants.CongNo.MaDoiTuong].ToString();
                    soducongno.TenDoiTuong = dr[Common.Constants.CongNo.TenDoiTuong].ToString();
                    soducongno.DiaChi = dr[Common.Constants.CongNo.DiaChi].ToString();
                    soducongno.SoDuDauKy = dr["SoDuDauKy"].ToString();
                    soducongno.NgayKetChuyen = DateTime.Parse(dr[Common.Constants.CongNo.NgayKetChuyen].ToString());
                    soducongno.SoDuCuoiKy = dr["SoDuCuoiKy"].ToString();
                    soducongno.LoaiDoiTuong = Boolean.Parse(dr[Common.Constants.CongNo.LoaiDoiTuong].ToString());
                    soducongno.MaKho = dr[Common.Constants.CongNo.MaKho].ToString();
                    soducongno.TrangThai = Boolean.Parse(dr[Common.Constants.CongNo.TrangThai].ToString());
                    arr.Add(soducongno);
                }
                int n = arr.Count;
                return (Entities.SoDuCongNo[])arr.ToArray(typeof(Entities.SoDuCongNo));
            }
            catch
            {
                return new Entities.SoDuCongNo[0];
            }
        }

        public static Entities.SoDuCongNo[] Select()
        {
            try
            {
                string sql = Common.Constants.CongNo.SelectSoDuCongNo;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.SoDuCongNo soducongno = new Entities.SoDuCongNo();
                    soducongno.SoDuCongNoID = Convert.ToInt32(dr[Common.Constants.CongNo.SoDuCongNoID].ToString());
                    soducongno.MaSoDuCongNo = dr[Common.Constants.CongNo.MaSoDuCongNo].ToString();
                    soducongno.MaDoiTuong = dr[Common.Constants.CongNo.MaDoiTuong].ToString();
                    soducongno.TenDoiTuong = dr[Common.Constants.CongNo.TenDoiTuong].ToString();
                    soducongno.DiaChi = dr[Common.Constants.CongNo.DiaChi].ToString();
                    soducongno.SoDuDauKy = dr["SoDuDauKy"].ToString();
                    soducongno.NgayKetChuyen = DateTime.Parse(dr[Common.Constants.CongNo.NgayKetChuyen].ToString());
                    soducongno.SoDuCuoiKy = dr["SoDuCuoiKy"].ToString();
                    soducongno.LoaiDoiTuong = Boolean.Parse(dr[Common.Constants.CongNo.LoaiDoiTuong].ToString());
                    soducongno.MaKho = dr[Common.Constants.CongNo.MaKho].ToString();
                    soducongno.TrangThai = Boolean.Parse(dr[Common.Constants.CongNo.TrangThai].ToString());
                    arr.Add(soducongno);
                }
                return (Entities.SoDuCongNo[])arr.ToArray(typeof(Entities.SoDuCongNo));
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region Insert - Update
        static double sono = 0;
        public static int Insert(Entities.SoDuCongNo soducongno)
        {
            int rt = 0;
            try
            {
                string maSoDuCongNo = "";
                Entities.GetID b = new Entities.GetID("SoDuCongNo", "");
                b = (Entities.GetID)BusinessLogic.GetID.SelectID(b);
                if (b.ColumnName == null)
                {
                    maSoDuCongNo = "SDCN_0001";
                }
                else
                {
                    maSoDuCongNo = Common.Utilities.ProcessID(b.ColumnName);
                }
                string sql = Common.Constants.CongNo.InsertSoDuCongNo;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.CongNo.MaSoDuCongNo, SqlDbType.VarChar, 20).Value = maSoDuCongNo;
                cmd.Parameters.Add(Common.Constants.CongNo.MaDoiTuong, SqlDbType.VarChar, 20).Value = soducongno.MaDoiTuong;
                cmd.Parameters.Add(Common.Constants.CongNo.TenDoiTuong, SqlDbType.NVarChar, 200).Value = soducongno.TenDoiTuong;
                cmd.Parameters.Add(Common.Constants.CongNo.DiaChi, SqlDbType.NVarChar, 200).Value = soducongno.DiaChi;
                cmd.Parameters.Add("SoDuDauKy", SqlDbType.Float).Value = soducongno.SoDuDauKy;
                cmd.Parameters.Add(Common.Constants.CongNo.NgayKetChuyen, SqlDbType.DateTime).Value = soducongno.NgayKetChuyen;
                cmd.Parameters.Add("SoDuCuoiKy", SqlDbType.Float).Value = soducongno.SoDuCuoiKy;
                cmd.Parameters.Add(Common.Constants.CongNo.LoaiDoiTuong, SqlDbType.Bit).Value = soducongno.LoaiDoiTuong;
                cmd.Parameters.Add(Common.Constants.CongNo.MaKho, SqlDbType.VarChar, 50).Value = soducongno.MaKho;
                cmd.Parameters.Add(Common.Constants.CongNo.TrangThai, SqlDbType.Bit).Value = soducongno.TrangThai;
                rt = cmd.ExecuteNonQuery();
                if (soducongno.LoaiDoiTuong == false)
                {
                    Entities.ChiTietCongNoKH temp = new Entities.ChiTietCongNoKH(
                        "1",
                        soducongno.MaDoiTuong,
                        soducongno.MaKho,
                        soducongno.SoDuDauKy,
                        false);
                    Entities.ChiTietCongNoKH[] chitiet = ChiTietCongNoKH.Select(temp);
                    if (chitiet != null && chitiet.Length != 0)
                        temp.HanhDong = "Update";
                    else
                        temp.HanhDong = "Insert";
                    rt = ChiTietCongNoKH.InsertUpdate(temp);
                }
                else
                {
                    Entities.ChiTietCongNoNCC temp = new Entities.ChiTietCongNoNCC();
                    temp.HanhDong = "1";
                    temp.MaKho = soducongno.MaKho;
                    temp.MaNhaCungCap = soducongno.MaDoiTuong;
                    temp.DuNo = soducongno.SoDuDauKy;
                    Entities.ChiTietCongNoNCC[] chitiet = ChiTietCongNoNCC.Select(temp);
                    if (chitiet != null && chitiet.Length != 0)
                        temp.HanhDong = "Update";
                    else
                        temp.HanhDong = "Insert";
                    rt = ChiTietCongNoNCC.InsertUpdate(temp);
                }
                return rt;
            }
            catch
            {
                return 0;
            }
        }

        public static int InsertKT(Entities.SoDuCongNo soducongno)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.CongNo.InsertSoDuCongNo;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.CongNo.MaSoDuCongNo, SqlDbType.VarChar, 20).Value = soducongno.MaSoDuCongNo;
                cmd.Parameters.Add(Common.Constants.CongNo.MaDoiTuong, SqlDbType.VarChar, 20).Value = soducongno.MaDoiTuong;
                cmd.Parameters.Add(Common.Constants.CongNo.TenDoiTuong, SqlDbType.NVarChar, 200).Value = soducongno.TenDoiTuong;
                cmd.Parameters.Add(Common.Constants.CongNo.DiaChi, SqlDbType.NVarChar, 200).Value = soducongno.DiaChi;
                cmd.Parameters.Add("SoDuDauKy", SqlDbType.Float).Value = soducongno.SoDuDauKy;
                cmd.Parameters.Add(Common.Constants.CongNo.NgayKetChuyen, SqlDbType.DateTime).Value = soducongno.NgayKetChuyen;
                cmd.Parameters.Add("SoDuCuoiKy", SqlDbType.Float).Value = soducongno.SoDuCuoiKy;
                cmd.Parameters.Add(Common.Constants.CongNo.LoaiDoiTuong, SqlDbType.Bit).Value = soducongno.LoaiDoiTuong;
                cmd.Parameters.Add(Common.Constants.CongNo.MaKho, SqlDbType.VarChar, 50).Value = soducongno.MaKho;
                cmd.Parameters.Add(Common.Constants.CongNo.TrangThai, SqlDbType.Bit).Value = soducongno.TrangThai;
                rt = cmd.ExecuteNonQuery();
                return rt;
            }
            catch
            {
                return 0;
            }
        }

        public static int InsertUpdate(Entities.SoDuCongNo[] soducongno)
        {
            TransactionScope ts = null;
            try
            {
                foreach (Entities.SoDuCongNo item in soducongno)
                {

                    using (ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        Insert(item);
                    }
                }
                return 1;
            }
            catch
            {
                ts.Dispose();
                return 0;
            }
        }

        public static int Update(Entities.SoDuCongNo pxh)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.CongNo.UpdateTinhTrangSoDuCongNo;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.CongNo.MaSoDuCongNo, SqlDbType.VarChar, 20).Value = pxh.MaSoDuCongNo;
                cmd.Parameters.Add("SoDuCuoiKy", SqlDbType.Float).Value = pxh.SoDuCuoiKy;
                cmd.Parameters.Add(Common.Constants.CongNo.LoaiDoiTuong, SqlDbType.Bit).Value = pxh.LoaiDoiTuong;
                rt = cmd.ExecuteNonQuery();
                return rt;
            }
            catch
            {
                return 0;
            }
        }

        public static bool Update(Entities.SoDuCongNo[] sdcn)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    int soluong = 0;
                    Entities.SoDuCongNo[] hienthi = Select(sdcn[0].MaKho);
                    // nếu obj null
                    int year1 = 0;
                    int month1 = 0;
                    if (hienthi == null)
                    {
                        hienthi = new Entities.SoDuCongNo[0];
                    }
                    else
                    {
                        bool k1 = false;
                        Entities.SoDuCongNo[] sdc = new Entities.SoDuCongNo[hienthi.Length];
                        // kiểm tra số dư hiện tại
                        for (int i = 0; i < sdc.Length; i++)
                        {
                            k1 = false;

                            int nam1 = sdcn[0].NgayKetChuyen.Year;
                            int thang1 = sdcn[0].NgayKetChuyen.Month;
                            if (nam1 == hienthi[i].NgayKetChuyen.Year && thang1 == hienthi[i].NgayKetChuyen.Month && sdcn[0].LoaiDoiTuong == hienthi[i].LoaiDoiTuong)
                            {
                                k1 = true;
                                break;
                            }
                        }

                        if (k1 == true)
                        {
                            for (int i = 0; i < sdc.Length; i++)
                            {
                                if (sdcn[0].NgayKetChuyen.Year == hienthi[i].NgayKetChuyen.Year && sdcn[0].NgayKetChuyen.Month == hienthi[i].NgayKetChuyen.Month && sdcn[0].LoaiDoiTuong == hienthi[i].LoaiDoiTuong)
                                {
                                    year1 = sdcn[0].NgayKetChuyen.Year;
                                    month1 = sdcn[0].NgayKetChuyen.Month;
                                    sdc[soluong] = hienthi[i];
                                    soluong++;
                                }
                            }
                            hienthi = new Entities.SoDuCongNo[soluong];
                            for (int i = 0; i < soluong; i++)
                            {
                                hienthi[i] = sdc[i];
                            }
                        }
                        else
                        {
                            for (int i = 0; i < sdc.Length; i++)
                            {
                                if (sdcn[0].NgayKetChuyen.Month != 1)
                                {
                                    if (sdcn[0].NgayKetChuyen.Year == hienthi[i].NgayKetChuyen.Year && (sdcn[0].NgayKetChuyen.Month - 1) == hienthi[i].NgayKetChuyen.Month && sdcn[0].LoaiDoiTuong == hienthi[i].LoaiDoiTuong)
                                    {
                                        Convert.ToDateTime((hienthi[0].NgayKetChuyen.Month - 1).ToString() + "/01/" + (hienthi[0].NgayKetChuyen.Year).ToString());
                                        year1 = hienthi[0].NgayKetChuyen.Year;
                                        month1 = hienthi[0].NgayKetChuyen.Month - 1;
                                        sdc[soluong] = hienthi[i];
                                        soluong++;
                                    }
                                }
                                else
                                {
                                    if ((sdcn[0].NgayKetChuyen.Year - 1) == hienthi[i].NgayKetChuyen.Year && 12 == hienthi[i].NgayKetChuyen.Month && sdcn[0].LoaiDoiTuong == hienthi[i].LoaiDoiTuong)
                                    {
                                        year1 = hienthi[0].NgayKetChuyen.Year;
                                        month1 = 12;
                                        hienthi[i].NgayKetChuyen = Convert.ToDateTime(12.ToString() + "/01/" + year1.ToString());
                                        sdc[soluong] = hienthi[i];
                                        soluong++;
                                    }
                                }
                            }
                            hienthi = new Entities.SoDuCongNo[soluong];
                            for (int i = 0; i < soluong; i++)
                            {
                                hienthi[i] = sdc[i];
                            }
                        }
                    }
                    Entities.SoDuCongNo[] mangluu;
                    int sotang = 0;
                    // đếm số bản ghi tồn tại rồi
                    for (int i = 0; i < hienthi.Length; i++)
                    {
                        if (hienthi[i].LoaiDoiTuong == sdcn[0].LoaiDoiTuong)
                        {
                            if (sdcn[0].MaDoiTuong != null)
                                sotang++;
                        }
                    }

                    if (hienthi.Length == 0)
                        mangluu = new Entities.SoDuCongNo[sotang];
                    else
                        mangluu = new Entities.SoDuCongNo[hienthi.Length];
                    sotang = 0;

                    // gán giá trị cho obj mangluu khởi tạo
                    if (hienthi.Length == 0)
                    {
                        for (int i = 0; i < hienthi.Length; i++)
                        {
                            if (hienthi[i].LoaiDoiTuong == sdcn[0].LoaiDoiTuong)
                            {
                                mangluu[sotang] = hienthi[i];
                                sotang++;
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < hienthi.Length; i++)
                        {
                            mangluu[i] = hienthi[i];
                        }
                    }
                    bool kt = false;
                    for (int i = 0; i < sdcn.Length; i++)
                    {
                        kt = false;
                        for (int j = 0; j < mangluu.Length; j++)
                        {
                            if (sdcn[i].MaDoiTuong == mangluu[j].MaDoiTuong && sdcn[0].LoaiDoiTuong == mangluu[j].LoaiDoiTuong)
                            {
                                kt = true;
                                break;
                            }
                        }
                        string maSoDuCongNo = "";
                        Entities.GetID b = new Entities.GetID("SoDuCongNo", "");
                        b = (Entities.GetID)BusinessLogic.GetID.SelectID(b);
                        if (b.ColumnName == null)
                        {
                            maSoDuCongNo = "SDCN_0001";
                        }
                        else
                        {
                            maSoDuCongNo = Common.Utilities.ProcessID(b.ColumnName);
                        }
                        if (kt == true)
                        {
                            sdcn[i].MaSoDuCongNo = hienthi[i].MaSoDuCongNo;
                            Update(sdcn[i]);
                            int nam = sdcn[i].NgayKetChuyen.Year;
                            int thang = sdcn[i].NgayKetChuyen.Month;
                            int namhientai = 0;
                            int thanghientai = 0;
                            if (hienthi.Length == 0)
                            {
                                namhientai = DateTime.Now.Year;
                                thanghientai = DateTime.Now.Month;
                            }
                            else
                            {
                                namhientai = hienthi[0].NgayKetChuyen.Year;
                                thanghientai = hienthi[0].NgayKetChuyen.Month;
                            }
                            Entities.SoDuCongNo a;
                            if (nam == namhientai && thang == thanghientai)
                            {
                                if (thang == 12)
                                    a = new Entities.SoDuCongNo(maSoDuCongNo, sdcn[i].MaDoiTuong, sdcn[i].TenDoiTuong, sdcn[i].DiaChi, sdcn[i].SoDuCuoiKy, Convert.ToDateTime("01/01/" + (namhientai + 1).ToString()), "0", sdcn[i].LoaiDoiTuong, false);
                                else
                                    a = new Entities.SoDuCongNo(maSoDuCongNo, sdcn[i].MaDoiTuong, sdcn[i].TenDoiTuong, sdcn[i].DiaChi, sdcn[i].SoDuCuoiKy, Convert.ToDateTime((thang + 1).ToString() + "/01/" + namhientai.ToString()), "0", sdcn[i].LoaiDoiTuong, false);
                            }
                            else
                                if (thanghientai == 12)
                                    a = new Entities.SoDuCongNo(maSoDuCongNo, sdcn[i].MaDoiTuong, sdcn[i].TenDoiTuong, sdcn[i].DiaChi, sdcn[i].SoDuCuoiKy, Convert.ToDateTime(thang.ToString() + "/01/" + (namhientai + 1).ToString()), "0", sdcn[i].LoaiDoiTuong, false);
                                else
                                    a = new Entities.SoDuCongNo(maSoDuCongNo, sdcn[i].MaDoiTuong, sdcn[i].TenDoiTuong, sdcn[i].DiaChi, sdcn[i].SoDuCuoiKy, DateTime.Now, "0", sdcn[i].LoaiDoiTuong, false);
                            a.MaKho = sdcn[i].MaKho;
                            InsertKT(a);
                        }
                        else
                        {
                            int nam = sdcn[i].NgayKetChuyen.Year;
                            int thang = sdcn[i].NgayKetChuyen.Month;
                            int namhientai = DateTime.Now.Year;
                            int thanghientai = DateTime.Now.Month;
                            Entities.SoDuCongNo a;
                            if (sdcn[0].MaDoiTuong != null)
                            {
                                a = new Entities.SoDuCongNo(maSoDuCongNo, sdcn[i].MaDoiTuong, sdcn[i].TenDoiTuong, sdcn[i].DiaChi, "0", Convert.ToDateTime(thang + "/01/" + nam), sdcn[i].SoDuCuoiKy, sdcn[i].LoaiDoiTuong, true);
                                a.MaKho = sdcn[i].MaKho;

                                InsertKT(a);
                                if (nam == namhientai && thang == thanghientai)
                                {
                                    if (thang == 12)
                                        a = new Entities.SoDuCongNo(Common.Utilities.ProcessID(maSoDuCongNo), sdcn[i].MaDoiTuong, sdcn[i].TenDoiTuong, sdcn[i].DiaChi, sdcn[i].SoDuCuoiKy, Convert.ToDateTime("01/01/" + (namhientai + 1).ToString()), "0", sdcn[i].LoaiDoiTuong, false);
                                    else
                                        a = new Entities.SoDuCongNo(Common.Utilities.ProcessID(maSoDuCongNo), sdcn[i].MaDoiTuong, sdcn[i].TenDoiTuong, sdcn[i].DiaChi, sdcn[i].SoDuCuoiKy, Convert.ToDateTime((thang + 1).ToString() + "/01/" + namhientai.ToString()), "0", sdcn[i].LoaiDoiTuong, false);
                                }
                                else
                                    if (thang == 12)
                                        a = new Entities.SoDuCongNo(Common.Utilities.ProcessID(maSoDuCongNo), sdcn[i].MaDoiTuong, sdcn[i].TenDoiTuong, sdcn[i].DiaChi, sdcn[i].SoDuCuoiKy, Convert.ToDateTime(thang.ToString() + "/01/" + (namhientai).ToString()), "0", sdcn[i].LoaiDoiTuong, false);
                                    else
                                        a = new Entities.SoDuCongNo(Common.Utilities.ProcessID(maSoDuCongNo), sdcn[i].MaDoiTuong, sdcn[i].TenDoiTuong, sdcn[i].DiaChi, sdcn[i].SoDuCuoiKy, DateTime.Now, "0", sdcn[i].LoaiDoiTuong, false);
                                a.MaKho = sdcn[i].MaKho;
                                InsertKT(a);
                            }
                            else
                            {
                                a = new Entities.SoDuCongNo(hienthi[i].MaSoDuCongNo, hienthi[i].MaDoiTuong, hienthi[i].TenDoiTuong, hienthi[i].DiaChi, "0", DateTime.Now, hienthi[i].SoDuDauKy, hienthi[i].LoaiDoiTuong, true);
                                Update(a);
                                if (nam == namhientai && thang == thanghientai)
                                {
                                    if (thang == 12)
                                        a = new Entities.SoDuCongNo(Common.Utilities.ProcessID(maSoDuCongNo), hienthi[i].MaDoiTuong, hienthi[i].TenDoiTuong, hienthi[i].DiaChi, hienthi[i].SoDuDauKy, Convert.ToDateTime("01/01/" + (namhientai + 1).ToString()), "0", hienthi[i].LoaiDoiTuong, false);
                                    else
                                        a = new Entities.SoDuCongNo(Common.Utilities.ProcessID(maSoDuCongNo), hienthi[i].MaDoiTuong, hienthi[i].TenDoiTuong, hienthi[i].DiaChi, hienthi[i].SoDuDauKy, Convert.ToDateTime((thang + 1).ToString() + "/01/" + namhientai.ToString()), "0", hienthi[i].LoaiDoiTuong, false);
                                }
                                else
                                    if (thang == 12)
                                        a = new Entities.SoDuCongNo(Common.Utilities.ProcessID(maSoDuCongNo), hienthi[i].MaDoiTuong, hienthi[i].TenDoiTuong, hienthi[i].DiaChi, hienthi[i].SoDuDauKy, Convert.ToDateTime(thang.ToString() + "/01/" + (namhientai).ToString()), "0", hienthi[i].LoaiDoiTuong, false);
                                    else
                                        a = new Entities.SoDuCongNo(Common.Utilities.ProcessID(maSoDuCongNo), hienthi[i].MaDoiTuong, hienthi[i].TenDoiTuong, hienthi[i].DiaChi, hienthi[i].SoDuDauKy, DateTime.Now, "0", hienthi[i].LoaiDoiTuong, false);
                                a.MaKho = sdcn[i].MaKho;
                                InsertKT(a);
                            }
                        }
                    }
                    ts.Complete();
                    return true;
                }
            }
            catch
            {
                ts.Dispose();
                return false;
            }
        }
        #endregion

        #region Delete
        public static int Delete(Entities.SoDuCongNo pxh)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.CongNo.DeleteSoDuCongNo;
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(Common.Constants.CongNo.MaSoDuCongNo, SqlDbType.VarChar, 50).Value = pxh.MaSoDuCongNo;
                    rt = cmd.ExecuteNonQuery();
                    if (rt != 0)
                    {
                        if (!pxh.LoaiDoiTuong)
                        {
                            Entities.ChiTietCongNoKH temp = new Entities.ChiTietCongNoKH(
                                "1",
                                pxh.MaDoiTuong,
                                pxh.MaKho,
                                pxh.SoDuDauKy,
                                false);
                            Entities.ChiTietCongNoKH[] chitiet = ChiTietCongNoKH.Select(temp);
                            if (chitiet != null && chitiet.Length != 0)
                                temp.HanhDong = "ThanhToan";
                            else
                                temp.HanhDong = "Insert";
                            rt = ChiTietCongNoKH.InsertUpdate(temp);
                        }
                        else
                        {
                            Entities.ChiTietCongNoNCC temp = new Entities.ChiTietCongNoNCC();
                            temp.HanhDong = "1";
                            temp.MaKho = pxh.MaKho;
                            temp.MaNhaCungCap = pxh.MaDoiTuong;
                            temp.DuNo = pxh.SoDuDauKy;
                            Entities.ChiTietCongNoNCC[] chitiet = ChiTietCongNoNCC.Select(temp);
                            if (chitiet != null && chitiet.Length != 0)
                                temp.HanhDong = "ThanhToan";
                            else
                                temp.HanhDong = "Insert";
                        }
                    }
                    ts.Complete();
                    return rt;
                }

            }
            catch
            {
                ts.Dispose();
                return 0;
            }
        }
        #endregion
    }
}
#endregion
